package vy.org.app.page.subscription.dabba;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.vy.dal.dao.VyAddressDO;
import org.vy.dal.dao.VyDabbaSubscriptionDO;

import vy.org.app.page.address.AddressHelper;

public class DabbaHelper {
	
	public static SimpleDateFormat _DATE_FMT = new SimpleDateFormat("MM/dd/yyyy");
	
	public static DabbaForm fillDabbaForm(VyDabbaSubscriptionDO dto, VyAddressDO addr){
		DabbaForm form = new DabbaForm();
		
		form.setDabbaKey(dto.getDabbaKey());
		form.setDabbaSubscriptionId(dto.getDabbaSubscriptionId());
		if(dto.getEndDate() != null)
			form.setEndDate(_DATE_FMT.format(dto.getEndDate()));
		form.setId(dto.getSubscriberId());
		if(dto.getIdType() != null)
			form.setIdType(dto.getIdType().toString());
		form.setIsActive(dto.getIsActive().toString());
		form.setLastUpdatedBy(dto.getLastUpdatedBy());
		form.setLastUpdatedOn(_DATE_FMT.format(dto.getLastUpdatedOn()));
		form.setRefVyId(dto.getRefVyId());
		form.setSankalpRashi(dto.getSankalpRashi());
		form.setStartDate(_DATE_FMT.format(dto.getStartDate()));
		form.setAddress(AddressHelper.getAddress(addr));
		
		return form;
	}

	public static VyDabbaSubscriptionDO fillDabbaDO(DabbaForm form) throws Exception {
		VyDabbaSubscriptionDO dto = new VyDabbaSubscriptionDO();
		
		dto.setDabbaKey(form.getDabbaKey());
		dto.setDabbaSubscriptionId(form.getDabbaSubscriptionId());
		if(form.getEndDate() != null && !"".equals(form.getEndDate()))
			dto.setEndDate(_DATE_FMT.parse(form.getEndDate()));
		dto.setSubscriberId(form.getId());
		if(form.getIdType() != null && !"".equals(form.getIdType()))
			dto.setIdType(Integer.parseInt(form.getIdType()));
		dto.setIsActive(Boolean.parseBoolean(form.getIsActive()));
		dto.setLastUpdatedBy(form.getLastUpdatedBy());
		dto.setLastUpdatedOn(new Date());
		dto.setRefVyId(form.getRefVyId());
		dto.setSankalpRashi(form.getSankalpRashi());
		if(form.getStartDate() != null && !"".equals(form.getStartDate()))
		dto.setStartDate(_DATE_FMT.parse(form.getStartDate()));
		if(form.getAddress()!=null && form.getAddress().getAddressId() != null
				&& !"".equals(form.getAddress().getAddressId()))
		dto.setAddressId(Integer.parseInt(form.getAddress().getAddressId()));
		
		return dto;
	}

}
